@extends('admin.layout.layout', ['row' => @$page])

@section('contain')
    <style>
        .table .tth{
            background-color: rgba(0, 0, 0, 0.05);
            width: 150px;
            font-weight: bold;
        }
        .table .ttd{
            width: 350px;
        }
        .table .ttt{
            padding: 0;
        }
        .ttt .table{
            margin: 0;
        }
        .ttt .table img + p{
            text-align: center;
            margin: 10px 0 0;
        }
        .tst{
            color: #00b050;
            font-weight: 700;
            text-align: center;
            font-size: 1.25rem;
        }
    </style>
    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>
        {{--<x-upload-box name="sfasdf"></x-upload-box>--}}
        <form action="/{{ request()->path() }}/../" method="post" onsubmit="return checkVal(this)" class="precut-form t-right">
            @csrf
            <input type="text" name="_previous_" value="{{ request()->header('referer') }}" hidden>
            <input type="text" name="_prec_" value="{{ $data->precut_id }}" hidden>
            <table class="table s3">
                <tbody>
                <tr>
                    <td class="tth">{{ ts('precutid') }}</td>
                    <td class="ttd">
                        <input type="text" readonly class="form-control" name="precut_id" value="{{ $data->precut_id }}">
                    </td>
                    <td colspan="4" class="text-danger"></td>
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('VEHICLECOVERAGE') }}</td>
                </tr>
                <tr>
                    <td colspan="6" class="ttt">
                        @include('admin.subview.coverage', ['coverages' => @getPrecutCoverages(@$data->id), 'is_precut'=>0, 'isprecut'=>1])
                        {{--<div class="container-fluid">
                            @include('admin.subview.precut_coverage', ['coverages' => getPrecutCoverages($data->id)])
                        </div>--}}
                    </td>
                </tr>
                </tbody>
            </table>

            <div class="mt-4">
                <button type="submit" class="btn btn-primary">{{ ts('store') }}</button>
            </div>
        </form>
    </div>


@endsection

@section('script')
    <script>

        $(document).ready(function () {
            // initCoverageRowsV2();
            initWVCRows();
        })

        /*$('body').on('blur change', '.precut-coverages .coverage-list .item [name="roll_number[]"]', function () {
            var val = $(this).val();
            val = String(val).trim()
            var $that = $(this);
            if (val) {
                request.get('/admin/ajax/getRestlen', {
                    params: {
                        q: val,
                        t: 'precut',
                    }
                })
                    .then( function(d) {
                        var data = d.data
                        $that.parents('.item').find('.residue-length b').attr('data-length', data.restlen)
                        $that.parents('.item').find('.residue-length').removeClass('d-none').show()
                        $that.parents('.item').find('[name="film_type_id[]"]').val(data.film_type_id)
                        $that.parents('.item').find('[name="width[]"]').val(data.width)
                        updatePVCRestlen()
                    })
                    .catch(function (d) {
                        console.log('catch', d)
                        toastr.error(d.msg)
                        if (d.error_code == 1004) {
                            // $that.val(' ').focus()
                        }
                        $that.parents('.item').find('.residue-length b').attr('data-length', 0)
                        $that.parents('.item').find('.residue-length').addClass('d-none')
                    })
            }
        })
        $('body').on('change input propertychange', '.precut-coverages .coverage-list .item [name="length[]"]', function () {
            var val = $(this).val()
            var roll_number = $(this).parents('.item').find('[name="roll_number[]"]').val()
            var max = $(this).parents('.item').find('.residue-length b').attr('data-length') || 0
            var used_length = getSameRollNumberUsedLength(roll_number) || 0
            val = Number(val) || 0
            max = Number(max) || 0
            residue_max = max-used_length+val
            console.log(max, used_length)
            if (val > residue_max) {
                val = residue_max
            }
            if (val < 0) {
                val = 0
            }
            $(this).val(val)
            updateRollNumberResidueLength()
        })
        $('body').on('click', '.precut-coverages .addmore', function () {
            makeNewPVCRow();
        })

        $('body').on('click', '.precut-coverages .coverage-list .item i.delete', function () {
            var len = $(this).parents('.item').siblings('.item').length
            if (len > 0) {
                $(this).parents('.item').remove();
            }
        })
        $('body').on('change', '[name="sdid"]', function () {
            var val=$(this).val();
            var $that = $(this)
            request.get('/admin/ajax/getUsers', {
                params: {
                    t: 'd',
                    user_id: val
                }
            })
                .then( function(d) {
                    var data = d.data
                    $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                })
                .catch(function (d) {
                    toastr.error(d.msg)
                })
        })
        $(document).ready(function () {
            {
                request.get('/admin/ajax/getUsers', {
                    params: {
                        t: 'd',
                        user_id: $('[name="sdid"]').val()
                    }
                })
                    .then( function(d) {
                        var data = d.data
                        $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                    })
                    .catch(function (d) {
                        toastr.error(d.msg)
                    })
            }
        })*/
    </script>
@endsection